package topopt;

import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Force;
import fem2.Mesh;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.material.TopOptFilter;
import fem2.material.TopOptSensitivityFilter;
import fem2.pre_and_post.PatchMeshGenerator;

public class MBBbeamTopOptDemo extends AbstractTopOptDemo {

	private int n1 = 61;
	private int n2 = 21;
	private double dx = 1.0;
	private double dy = 1.0;

	public MBBbeamTopOptDemo() {
		projectName = "topopt.gid";
	}

	@Override
	public TopOptFilter createFilter(Model m) {
		double rmin = 1.5;
		TopOptFilter filter = new TopOptSensitivityFilter(m, rmin);
		return filter;
	}

	@Override
	public Mesh createMesh() {
		PatchMeshGenerator p = new PatchMeshGenerator(n1, n2, 0, 0, dx, dy);
		return p.getMesh(2);
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();
		Constraint c1 = new Constraint(false, true);
		Constraint c2 = new Constraint(true, false);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 1.0, 0.0, 0.0));
		MeshUtilities.seekNode(mesh, (n1 - 1) * dx, 0.0).setConstraint(c2);

		Node nf = MeshUtilities.seekNode(mesh, 0.0, (n2 - 1) * dy);
		double F = -1.0;
		Force f = new Force(nf, 0, F);
		m.addLoad(f);
		return m;
	}

	public static void main(String[] args) {
		AbstractStructuralStaticDemo demo = new MBBbeamTopOptDemo();

		demo.run();
	}
}
